#include  <mian.h>

int key_flag=0;
int key_s(void)
{
	unsigned char key1=0x00,key2=0x00,key_tmp=0,key_num=0;
	P3=0XF0;
	if(P37==0)key1=0x70;
	if(P36==0)key1=0xb0;
	if(P35==0)key1=0xd0;
	if(P34==0)key1=0xe0;
	if(P34==1 && P35==1 && P36==1 && P37==1)key1=0xf0;
	
	P3=0X0F;
	if(P33==0)key2=0x07;
	if(P32==0)key2=0x0b;
	if(P31==0)key2=0x0d;
	if(P30==0)key2=0x0e;
	if(P30==1 && P31==1 && P32==1 && P33==1)key2=0x0f;
	
	key_tmp=key1|key2;
	
	switch(key_flag)
	{
		case 0:if(key_tmp!=0xff)key_flag=1;break;
		case 1:if(key_tmp==0xff)key_flag=0;
						else 
						{
							switch(key_tmp)
							{
								case 0x77:key_num=16;break;
								case 0x7b:key_num=12;break;
								case 0x7d:key_num=8;break;
								case 0x7e:key_num=4;break;
								
								case 0xb7:key_num=15;break;
								case 0xbb:key_num=11;break;
								case 0xbd:key_num=7;break;
								case 0xbe:key_num=3;break;
								
								case 0xd7:key_num=14;break;
								case 0xdb:key_num=10;break;
								case 0xdd:key_num=6;break;
								case 0xde:key_num=2;break;
								
								case 0xe7:key_num=13;break;
								case 0xeb:key_num=9;break;
								case 0xed:key_num=5;break;
								case 0xee:key_num=1;break;
							}key_flag=2;
						}break;
		case 2: if(key_tmp==0xff)key_flag=0; break;
	}
	return key_num;
}